package com.ubuntuvim.annotaction;

import java.lang.reflect.Method;

/**
 * The reflection mechanism used in analytical annotation method or class commentary.
 * @author cdq@fortunes.com.cn
 */
public class ParseAnnotation {
	public static void main(String[] args) {
		try {
			Object obj = Class.forName("com.ubuntuvim.annotaction.MyAnnotationTest").newInstance();
//			Annotation[] ants = obj.getClass().getDeclaredAnnotations();  //  get the notes in class,interface, enumeration commentray.
//			for (Annotation a : ants) {
//				System.out.println(a.toString());
//			}
			MyAnnotation01 an1 = obj.getClass().getAnnotation(MyAnnotation01.class);
			System.out.println(an1.clsType());
			if (MyAnnotation01.ClsType.dynCls.equals(an1.clsType())) {
				System.out.println("This is a dynamic class ...");
			} else if (MyAnnotation01.ClsType.staticCls.equals(an1.clsType())) {
				System.out.println("This is a static class...");
			} else {
				System.out.println("Other types of class...");
			}
			
			
			Method[] mts = obj.getClass().getDeclaredMethods();
			for (Method m : mts) {
				if (m.isAnnotationPresent(MyAnnotation02.class)) {
					MyAnnotation02 na2 = m.getAnnotation(MyAnnotation02.class);
					String description = na2.description();
					boolean isAnnotation = na2.isAnnotation();
					System.out.println("Description of the methods for: " + description);
					if (isAnnotation) {
						System.out.println("the isAnnotation is true");
					} else {
						System.out.println("the isAnnotation is false");
					}
					//  Execution method using reflection
//					try {
//						m.invoke(obj, "Parameter of method...");
//					} catch (IllegalArgumentException e) {
//						e.printStackTrace();
//					} catch (InvocationTargetException e) {
//						e.printStackTrace();
//					}
				}
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
